package com.gisocn.onemap.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.gisocn.onemap.base.BaseFragment;
import com.gisocn.onemap.base.CommonAdapter;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.base.MyApplication;
import com.gisocn.onemap.base.ViewHolder;
import com.gisocn.onemap.event.MessageEvent;
import com.gisocn.onemap.greendao.OfflineDownLoadFileInfoDao;
import com.gisocn.onemap.interfaces.DownLoadListener;
import com.gisocn.onemap.model.AppConfig;
import com.gisocn.onemap.model.Download;
import com.gisocn.onemap.model.LoadResponseHead;
import com.gisocn.onemap.model.LocalFileInfo;
import com.gisocn.onemap.presenter.OfflineDownloadPresenter;
import com.gisocn.onemap.sgzx.R;
import com.gisocn.onemap.ui.activity.MainActivity;
import com.gisocn.onemap.ui.adapter.OfflineDownloadAdapter;
import com.gisocn.onemap.utils.FileUtils;
import com.gisocn.onemap.utils.FormatSizeUtil;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.utils.SpUtils;
import com.gisocn.onemap.view.OfflineDownloadView;
import com.gisocn.onemap.widget.MyProgress;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.greenrobot.eventbus.EventBus;

/* loaded from: classes2.dex */
public class OfflineDownloadMultiLevelFragment extends BaseFragment<OfflineDownloadPresenter> implements OfflineDownloadView {
    public static final int EXTERNAL_STORAGE_REQ_CODE = 10;
    private static Gson gson = new Gson();
    private OfflineDownloadAdapter adapter;
    private AppConfig appConfig;
    private CommonAdapter downloadChildAdapter;
    private CommonAdapter downloadadapter;
    OfflineDownloadPresenter presenter;
    @BindView(R.id.recycle_offline_download)
    RecyclerView recycleDownload;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    Unbinder unbinder;
    private ArrayList<Download.NewItem> xmlList = new ArrayList<>();
    private ArrayList<Download.GroupItem> groupItemList = new ArrayList<>();
    ArrayList<Download.NewItem> downloadInfoList = new ArrayList<>();
    private Map<String, Download.NewItem> dataMaps = new LinkedHashMap();
    private Map<String, Download.GroupItem> groupMaps = new LinkedHashMap();
    private Map<String, Download.NewItem> downloadingList = new LinkedHashMap();
    private OfflineDownLoadFileInfoDao fileInfoDao = MyApplication.getInstances().getDaoSession().getOfflineDownLoadFileInfoDao();
    boolean isShowProgress = false;
    private boolean loadingFinish = false;
    private ArrayList<LocalFileInfo> localFileInfos = new ArrayList<>();
    HashMap<String, Long> localFileInfoMaps = new HashMap<>();
    Handler handler = new Handler() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.4
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
            if (i != 1) {
                if (i == 2) {
                    OfflineDownloadMultiLevelFragment.this.downloadadapter.notifyDataSetChanged();
                    if (message.arg1 == 100) {
                        OfflineDownloadMultiLevelFragment.this.downloadadapter.notifyDataSetChanged();
                        OfflineDownloadMultiLevelFragment.this.updateCounts();
                        EventBus.getDefault().post(new MessageEvent(Constant.MESSAGE_EVENT_UPDATA_LAYER_DATA));
                        return;
                    }
                    return;
                } else if (i == 3) {
                    ((Download.NewItem) OfflineDownloadMultiLevelFragment.this.xmlList.get(message.arg1)).setVersionDesc("已是最新版本");
                    return;
                } else if (i != 4) {
                    return;
                } else {
                    OfflineDownloadMultiLevelFragment.this.dismissProgressDialog();
                    OfflineDownloadMultiLevelFragment.this.showToast("下载失败");
                    return;
                }
            }
            Iterator it = OfflineDownloadMultiLevelFragment.this.xmlList.iterator();
            while (it.hasNext()) {
                Download.NewItem newItem = (Download.NewItem) it.next();
                if (OfflineDownloadMultiLevelFragment.this.groupMaps.containsKey(newItem.getGroup())) {
                    ((Download.GroupItem) OfflineDownloadMultiLevelFragment.this.groupMaps.get(newItem.getGroup())).getItems().add(newItem);
                }
            }
            OfflineDownloadMultiLevelFragment.this.groupItemList.addAll(OfflineDownloadMultiLevelFragment.this.groupMaps.values());
            OfflineDownloadMultiLevelFragment.this.dismissProgressDialog();
            OfflineDownloadMultiLevelFragment.this.loadingFinish = true;
            Log.e("xmlList.size()", OfflineDownloadMultiLevelFragment.this.xmlList.size() + "");
            if (OfflineDownloadMultiLevelFragment.this.downloadadapter != null) {
                OfflineDownloadMultiLevelFragment.this.downloadadapter.notifyDataSetChanged();
            }
            OfflineDownloadMultiLevelFragment.this.updateCounts();
        }
    };

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void initData() {
    }

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void onInvisible() {
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_offline_download_multilevel, viewGroup, false);
        this.unbinder = ButterKnife.bind(this, inflate);
        this.tvTitle.setText(R.string.offline_download);
        this.tvRight.setVisibility(8);
        this.isShowProgress = false;
        initDatas();
        return inflate;
    }

    void initDatas() {
        getLocalFileInfos();
        this.appConfig = ((MainActivity) getActivity()).getAppConfig();
        this.presenter = createPresenter();
        if (this.appConfig.getDownload().getItems() != null && this.appConfig.getDownload().getItems().size() > 0) {
            this.xmlList.clear();
            this.dataMaps.clear();
            this.groupMaps.clear();
            this.groupItemList.clear();
            Iterator<Download.NewItem> it = this.appConfig.getDownload().getItems().iterator();
            while (it.hasNext()) {
                Download.NewItem next = it.next();
                if (!"0".equals(next.getType())) {
                    next.setVersionDesc("未下载");
                    String str = next.getcData().trim().split("/")[next.getcData().trim().split("/").length - 1];
                    if (!this.dataMaps.containsKey(str)) {
                        if (next.getType().equals("shapefile")) {
                            File file = new File(Constant.SAVE_PATH_SHP);
                            if (file.exists()) {
                                File[] listFiles = file.listFiles();
                                if (listFiles == null) {
                                    next.setVersionDesc("未下载");
                                } else if (listFiles.length > 0) {
                                    if (listFiles.length == 1 && listFiles[0].getName().contains(".temp")) {
                                        next.setVersionDesc("未下载");
                                    } else {
                                        next.setLastModifiedTime(Long.valueOf(file.lastModified()));
                                    }
                                } else {
                                    next.setVersionDesc("未下载");
                                }
                            } else {
                                next.setVersionDesc("未下载");
                            }
                        }
                        this.dataMaps.put(str, next);
                    }
                } else if (!this.groupMaps.containsKey(next.getName())) {
                    Download.GroupItem groupItem = new Download.GroupItem();
                    groupItem.setName(next.getName());
                    groupItem.setGroupId(next.getGroup());
                    groupItem.setItems(new ArrayList<>());
                    this.groupMaps.put(next.getGroup(), groupItem);
                }
            }
            L.e("本地文件长度:" + this.presenter.getLocalPathFile(Constant.SAVE_PATH_TPK, null).size());
            ArrayList arrayList = new ArrayList();
            arrayList.addAll(this.presenter.getLocalPathFile(Constant.SAVE_PATH_TPK, null));
            Iterator it2 = arrayList.iterator();
            while (it2.hasNext()) {
                File file2 = (File) it2.next();
                if (this.dataMaps.containsKey(file2.getName())) {
                    this.dataMaps.get(file2.getName()).setLastModifiedTime(Long.valueOf(file2.lastModified()));
                }
            }
            this.xmlList.addAll(this.dataMaps.values());
            if (this.isShowProgress) {
                showProgressDialog();
            }
            if (ActivityCompat.checkSelfPermission(getActivity(), "android.permission.WRITE_EXTERNAL_STORAGE") == 0) {
                new Thread(new Runnable() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.1
                    @Override // java.lang.Runnable
                    public void run() {
                        OfflineDownloadMultiLevelFragment.this.presenter.getDownLoadInfo(((Download.NewItem) OfflineDownloadMultiLevelFragment.this.xmlList.get(0)).getcData().trim(), 0);
                    }
                }).start();
            } else {
                showToast("请设置文件读写权限");
            }
        }
        initView();
    }

    void initView() {
        dismissProgressDialog();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(1);
        this.recycleDownload.setLayoutManager(linearLayoutManager);
        AnonymousClass2 anonymousClass2 = new AnonymousClass2(getActivity(), R.layout.item_offline_download_multilevel, this.groupItemList);
        this.downloadadapter = anonymousClass2;
        this.recycleDownload.setAdapter(anonymousClass2);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment$2  reason: invalid class name */
    /* loaded from: classes2.dex */
    public class AnonymousClass2 extends CommonAdapter<Download.GroupItem> {
        AnonymousClass2(Context context, int i, List list) {
            super(context, i, list);
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // com.gisocn.onemap.base.CommonAdapter
        public void convert(ViewHolder viewHolder, final Download.GroupItem groupItem, final int i) {
            viewHolder.setText(R.id.tv_expand_group_title, groupItem.getName());
            viewHolder.setVisible(R.id.recycle_download_child, groupItem.isUnfold());
            viewHolder.setImageResource(R.id.iv_expand_group_arrow, groupItem.isUnfold() ? R.mipmap.icon_expand_group_down : R.mipmap.icon_expand_group_right);
            Iterator<Download.NewItem> it = groupItem.getItems().iterator();
            int i2 = 0;
            while (it.hasNext()) {
                if (!"已是最新版本".equals(it.next().getVersionDesc())) {
                    i2++;
                }
            }
            viewHolder.setVisible(R.id.tv_expand_group_number, i2 > 0);
            viewHolder.setText(R.id.tv_expand_group_number, i2 + "");
            viewHolder.getView(R.id.tv_expand_group_download_all).setEnabled(i2 > 0);
            viewHolder.setText(R.id.tv_expand_group_download_all, i2 > 0 ? "下载全部" : "已完成");
            viewHolder.setTextColor(R.id.tv_expand_group_download_all, ContextCompat.getColor(OfflineDownloadMultiLevelFragment.this.getActivity(), i2 > 0 ? R.color.colorPrimary : R.color.colorBgGray));
            viewHolder.setBackgroundRes(R.id.tv_expand_group_download_all, i2 > 0 ? R.drawable.bg_primary_radius5_storck : R.drawable.bg_black_radius5_storck);
            viewHolder.setOnClickListener(R.id.tv_expand_group_download_all, new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.2.1
                @Override // android.view.View.OnClickListener
                public void onClick(View view) {
                    Iterator<Download.NewItem> it2 = groupItem.getItems().iterator();
                    while (it2.hasNext()) {
                        Download.NewItem next = it2.next();
                        if (!"已是最新版本".equals(next.getVersionDesc()) && !next.isDownloading()) {
                            if (!OfflineDownloadMultiLevelFragment.this.downloadingList.containsKey(next.getName())) {
                                next.setCurrentLength("0");
                                next.setDownloading(true);
                                OfflineDownloadMultiLevelFragment.this.downloadadapter.notifyItemChanged(i);
                            }
                            OfflineDownloadMultiLevelFragment.this.downLoadOfflineFile(next, i, OfflineDownloadMultiLevelFragment.this.recycleDownload, false);
                        }
                    }
                }
            });
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.2.2
                @Override // android.view.View.OnClickListener
                public void onClick(View view) {
                    Download.GroupItem groupItem2 = groupItem;
                    groupItem2.setUnfold(!groupItem2.isUnfold());
                    AnonymousClass2.this.notifyDataSetChanged();
                }
            });
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(OfflineDownloadMultiLevelFragment.this.getActivity());
            linearLayoutManager.setOrientation(1);
            ((RecyclerView) viewHolder.getView(R.id.recycle_download_child)).setLayoutManager(linearLayoutManager);
            OfflineDownloadMultiLevelFragment offlineDownloadMultiLevelFragment = OfflineDownloadMultiLevelFragment.this;
            offlineDownloadMultiLevelFragment.downloadChildAdapter = new AnonymousClass3(offlineDownloadMultiLevelFragment.getActivity(), R.layout.layout_offline_download_item_child, groupItem.getItems(), i);
            ((RecyclerView) viewHolder.getView(R.id.recycle_download_child)).setAdapter(OfflineDownloadMultiLevelFragment.this.downloadChildAdapter);
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        /* renamed from: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment$2$3  reason: invalid class name */
        /* loaded from: classes2.dex */
        public class AnonymousClass3 extends CommonAdapter<Download.NewItem> {
            final /* synthetic */ int val$position;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            AnonymousClass3(Context context, int i, List list, int i2) {
                super(context, i, list);
                this.val$position = i2;
            }

            /* JADX INFO: Access modifiers changed from: protected */
            @Override // com.gisocn.onemap.base.CommonAdapter
            public void convert(final ViewHolder viewHolder, final Download.NewItem newItem, final int i) {
                TextView textView = (TextView) viewHolder.getView(R.id.btn_offline_download);
                MyProgress myProgress = (MyProgress) viewHolder.getView(R.id.progress);
                viewHolder.setText(R.id.tv_title, newItem.getName() + "");
                if (!OfflineDownloadMultiLevelFragment.this.isNull(newItem.getSize())) {
                    viewHolder.setText(R.id.tv_size, FormatSizeUtil.getFormatSize(Double.parseDouble(newItem.getSize())) + "");
                } else {
                    viewHolder.setText(R.id.tv_size, newItem.getSize() + "");
                }
                viewHolder.setText(R.id.tv_offline_download_status, newItem.getVersionDesc() + "");
                if (newItem.isDownloading()) {
                    viewHolder.setText(R.id.tv_offline_download_status, "下载中...");
                    viewHolder.setTextColor(R.id.tv_offline_download_status, ContextCompat.getColor(OfflineDownloadMultiLevelFragment.this.getActivity(), R.color.colorBlue));
                    viewHolder.setText(R.id.tv_progress, newItem.getCurrentLength() + "%");
                    viewHolder.setVisible(R.id.progress, true);
                    textView.setVisibility(4);
                    if (!OfflineDownloadMultiLevelFragment.this.isNull(newItem.getCurrentLength())) {
                        myProgress.setProgress(Integer.parseInt(newItem.getCurrentLength()));
                    }
                } else {
                    viewHolder.setTextColor(R.id.tv_offline_download_status, ContextCompat.getColor(OfflineDownloadMultiLevelFragment.this.getActivity(), R.color.colorBlack));
                    viewHolder.setText(R.id.tv_progress, "");
                    textView.setVisibility(0);
                    viewHolder.setVisible(R.id.progress, false);
                    myProgress.setProgress(0);
                }
                textView.setBackgroundResource(R.drawable.bg_primary_radius5_storck);
                textView.setTextColor(ContextCompat.getColor(OfflineDownloadMultiLevelFragment.this.getActivity(), R.color.colorPrimary));
                if ("未下载".equals(newItem.getVersionDesc())) {
                    textView.setText("下载");
                } else if ("已是最新版本".equals(newItem.getVersionDesc())) {
                    textView.setText("已完成");
                    textView.setBackgroundResource(R.drawable.bg_black_radius5_storck);
                    textView.setTextColor(ContextCompat.getColor(OfflineDownloadMultiLevelFragment.this.getActivity(), R.color.colorBgGray));
                    textView.setEnabled(false);
                } else if ("有更新版本".equals(newItem.getVersionDesc())) {
                    textView.setText("更新");
                } else if ("未完成".equals(newItem.getVersionDesc())) {
                    viewHolder.setText(R.id.btn_offline_download, "下载");
                    textView.setText("下载");
                    viewHolder.setText(R.id.tv_progress, "");
                    viewHolder.setVisible(R.id.progress, true);
                    myProgress.setProgress(0);
                }
                textView.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.2.3.1
                    @Override // android.view.View.OnClickListener
                    public void onClick(View view) {
                        L.e("点击下载" + newItem.getName());
                        if (!OfflineDownloadMultiLevelFragment.this.downloadingList.containsKey(newItem.getName())) {
                            newItem.setCurrentLength("0");
                            newItem.setDownloading(true);
                            OfflineDownloadMultiLevelFragment.this.downloadChildAdapter.notifyItemChanged(i);
                            L.e("点击下载" + newItem.getName() + "aaaa");
                        }
                        OfflineDownloadMultiLevelFragment.this.downLoadOfflineFile(newItem, i, (RecyclerView) viewHolder.getView(R.id.recycle_download_child), true);
                    }
                });
                viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.2.3.2
                    @Override // android.view.View.OnLongClickListener
                    public boolean onLongClick(View view) {
                        if ("已是最新版本".equals(newItem.getVersionDesc())) {
                            RelativeLayout relativeLayout = (RelativeLayout) LayoutInflater.from(OfflineDownloadMultiLevelFragment.this.getActivity()).inflate(R.layout.dialog_clean_message, (ViewGroup) null);
                            AlertDialog.Builder builder = new AlertDialog.Builder(OfflineDownloadMultiLevelFragment.this.getActivity());
                            builder.setView(relativeLayout);
                            final AlertDialog create = builder.create();
                            create.setCancelable(false);
                            create.show();
                            ((TextView) relativeLayout.findViewById(R.id.tv_content)).setText("是否删除本地\"" + newItem.getName() + "\"资源文件？");
                            TextView textView2 = (TextView) relativeLayout.findViewById(R.id.btn_clean_message_ok);
                            textView2.setText("删除");
                            TextView textView3 = (TextView) relativeLayout.findViewById(R.id.btn_clean_message_cancel);
                            textView3.setText("取消");
                            textView3.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.2.3.2.1
                                @Override // android.view.View.OnClickListener
                                public void onClick(View view2) {
                                    create.dismiss();
                                }
                            });
                            textView2.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.2.3.2.2
                                @Override // android.view.View.OnClickListener
                                public void onClick(View view2) {
                                    String trim = newItem.getcData().trim();
                                    if (OfflineDownloadMultiLevelFragment.this.deleteLocalData(trim.split("/")[trim.split("/").length - 1])) {
                                        OfflineDownloadMultiLevelFragment.this.showToast("文件删除成功!");
                                        newItem.setVersionDesc("未下载");
                                        OfflineDownloadMultiLevelFragment.this.downloadadapter.notifyItemChanged(AnonymousClass3.this.val$position);
                                    } else {
                                        OfflineDownloadMultiLevelFragment.this.showToast("文件删除失败!");
                                    }
                                    create.dismiss();
                                }
                            });
                            return true;
                        }
                        return true;
                    }
                });
            }
        }
    }

    void downLoadOfflineFile(final Download.NewItem newItem, final int i, final RecyclerView recyclerView, final boolean z) {
        if (this.downloadingList.containsKey(newItem.getName())) {
            showToast("加载中...");
            return;
        }
        this.downloadingList.put(newItem.getName(), newItem);
        String trim = newItem.getcData().trim();
        if ((trim.split("/")[trim.split("/").length - 1] + ".temp").contains(".tpk")) {
            String str = Constant.SAVE_PATH_TPK;
        } else {
            String str2 = Constant.SAVE_PATH_SHP;
        }
        this.presenter.getDownLoading(newItem.getcData().trim(), new DownLoadListener() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.3
            @Override // com.gisocn.onemap.interfaces.DownLoadListener
            public void onStart() {
                L.e("onStart____________________");
                newItem.setDownloading(true);
            }

            @Override // com.gisocn.onemap.interfaces.DownLoadListener
            public void onProgress(int i2) {
                if (OfflineDownloadMultiLevelFragment.this.isNull(newItem.getCurrentLength()) || i2 > Integer.parseInt(newItem.getCurrentLength())) {
                    OfflineDownloadMultiLevelFragment.this.dismissProgressDialog();
                    Download.NewItem newItem2 = newItem;
                    newItem2.setCurrentLength("" + i2);
                    if (i2 == 100) {
                        newItem.setVersionDesc("已是最新版本");
                        newItem.setDownloading(false);
                    }
                    Message message = new Message();
                    message.what = 2;
                    message.arg1 = i2;
                    message.arg2 = i;
                    message.obj = Boolean.valueOf(z);
                    OfflineDownloadMultiLevelFragment.this.handler.sendMessage(message);
                }
            }

            @Override // com.gisocn.onemap.interfaces.DownLoadListener
            public void onFinish(String str3) {
                newItem.setDownloading(false);
                Message message = new Message();
                message.what = 3;
                message.arg1 = i;
                message.obj = recyclerView;
                OfflineDownloadMultiLevelFragment.this.handler.sendMessage(message);
            }

            @Override // com.gisocn.onemap.interfaces.DownLoadListener
            public void onFailure() {
                newItem.setDownloading(false);
                OfflineDownloadMultiLevelFragment.this.handler.sendEmptyMessage(4);
            }
        });
    }

    @Override // androidx.fragment.app.Fragment
    public void onStop() {
        super.onStop();
        dismissProgressDialog();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseFragment
    public OfflineDownloadPresenter createPresenter() {
        return new OfflineDownloadPresenter(this, getActivity());
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public void onDestroyView() {
        super.onDestroyView();
        this.unbinder.unbind();
        OfflineDownloadPresenter offlineDownloadPresenter = this.presenter;
        if (offlineDownloadPresenter != null) {
            offlineDownloadPresenter.closeDownloading();
        }
        this.handler.removeCallbacksAndMessages(null);
    }

    @OnClick({R.id.tv_close, R.id.tv_right})
    public void onViewClicked(View view) {
        if (view.getId() != R.id.tv_close) {
            return;
        }
        getActivity().findViewById(R.id.viewPager).setVisibility(8);
    }

    @Override // com.gisocn.onemap.view.OfflineDownloadView
    public void getDownLoadSuccess(String str, int i) {
        this.localFileInfoMaps.clear();
        Gson gson2 = new Gson();
        ArrayList arrayList = (ArrayList) gson2.fromJson(SpUtils.get(getActivity(), "downloadInfo", "") + "", new TypeToken<List<LocalFileInfo>>() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.5
        }.getType());
        if (arrayList != null) {
            Iterator it = arrayList.iterator();
            boolean z = false;
            while (it.hasNext()) {
                LocalFileInfo localFileInfo = (LocalFileInfo) it.next();
                if (str.equals(localFileInfo.getPath())) {
                    z = true;
                    localFileInfo.setLastModified(System.currentTimeMillis());
                }
                this.localFileInfoMaps.put(localFileInfo.getPath(), Long.valueOf(localFileInfo.getLastModified()));
            }
            if (!z) {
                LocalFileInfo localFileInfo2 = new LocalFileInfo();
                localFileInfo2.setPath(str);
                localFileInfo2.setLastModified(System.currentTimeMillis());
                arrayList.add(localFileInfo2);
            }
        } else {
            arrayList = new ArrayList();
            LocalFileInfo localFileInfo3 = new LocalFileInfo();
            localFileInfo3.setPath(str);
            localFileInfo3.setLastModified(System.currentTimeMillis());
            arrayList.add(localFileInfo3);
            this.localFileInfoMaps.put(str, Long.valueOf(System.currentTimeMillis()));
        }
        this.localFileInfos.clear();
        this.localFileInfos.addAll(arrayList);
        SpUtils.put(getActivity(), "downloadInfo", new Gson().toJson(this.localFileInfos));
    }

    void getLocalFileInfos() {
        L.e("getLocalFileInfos");
        this.localFileInfoMaps.clear();
        Gson gson2 = new Gson();
        ArrayList arrayList = (ArrayList) gson2.fromJson(SpUtils.get(getActivity(), "downloadInfo", "") + "", new TypeToken<List<LocalFileInfo>>() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadMultiLevelFragment.6
        }.getType());
        if (arrayList != null) {
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                LocalFileInfo localFileInfo = (LocalFileInfo) it.next();
                this.localFileInfoMaps.put(localFileInfo.getPath(), Long.valueOf(localFileInfo.getLastModified()));
            }
            File[] listFiles = new File(Constant.SAVE_PATH_TPK).listFiles();
            if (listFiles == null) {
                return;
            }
            for (int i = 0; i < listFiles.length; i++) {
                if (this.localFileInfoMaps.containsKey(listFiles[i].getAbsolutePath()) && this.localFileInfoMaps.get(listFiles[i].getAbsolutePath()).longValue() < listFiles[i].lastModified()) {
                    this.localFileInfoMaps.put(listFiles[i].getAbsolutePath(), Long.valueOf(listFiles[i].lastModified()));
                }
            }
            arrayList.clear();
            for (Map.Entry<String, Long> entry : this.localFileInfoMaps.entrySet()) {
                LocalFileInfo localFileInfo2 = new LocalFileInfo();
                localFileInfo2.setPath(entry.getKey());
                localFileInfo2.setLastModified(entry.getValue().longValue());
                arrayList.add(localFileInfo2);
            }
            this.localFileInfos.addAll(arrayList);
            SpUtils.put(getActivity(), "downloadInfo", new Gson().toJson(this.localFileInfos));
        }
    }

    @Override // com.gisocn.onemap.view.OfflineDownloadView
    public void getDownLoadInfoSuccess(LoadResponseHead loadResponseHead, int i) {
        String str = this.xmlList.get(i).getcData().trim().split("/")[this.xmlList.get(i).getcData().trim().split("/").length - 1];
        if (!isNull(loadResponseHead.getLast_modified())) {
            String last_modified = loadResponseHead.getLast_modified();
            this.dataMaps.get(str).setVersion(String.valueOf(new Date(last_modified).getTime()));
            this.xmlList.get(i).setVersion(String.valueOf(new Date(last_modified).getTime()));
        }
        if (!isNull(loadResponseHead.getContent_length())) {
            String content_length = loadResponseHead.getContent_length();
            this.dataMaps.get(str).setSize(content_length);
            this.xmlList.get(i).setSize(content_length);
        }
        int i2 = i + 1;
        if (i2 <= this.xmlList.size() - 1) {
            if (this.xmlList.get(i2).getcData() != null) {
                this.presenter.getDownLoadInfo(this.xmlList.get(i2).getcData().trim(), i2);
                return;
            }
            int i3 = i2 + 1;
            this.presenter.getDownLoadInfo(this.xmlList.get(i3).getcData().trim(), i3);
            return;
        }
        Iterator<Download.NewItem> it = this.xmlList.iterator();
        while (it.hasNext()) {
            Download.NewItem next = it.next();
            String substring = next.getcData().substring(next.getcData().lastIndexOf("/") + 1);
            String str2 = substring.contains(".zip") ? Constant.SAVE_PATH_SHP : Constant.SAVE_PATH_TPK;
            File file = new File(str2, substring + ".temp");
            File file2 = new File(substring.contains(".zip") ? Constant.SAVE_PATH_SHP : Constant.SAVE_PATH_TPK, substring);
            if (next.getLastModifiedTime() == null || (file.exists() && !file2.exists())) {
                next.setVersionDesc("未下载");
                if (file.exists() && substring.contains(".zip")) {
                    File file3 = new File(Constant.SAVE_PATH_SHP);
                    if (file3.exists() && file3.listFiles().length > 1) {
                        next.setVersionDesc("有更新版本");
                    } else {
                        next.setVersionDesc("未下载");
                    }
                }
            } else if (file.exists() && file2.exists()) {
                next.setVersionDesc("有更新版本");
            } else if (next.getVersion() == null) {
                next.setVersionDesc("已是最新版本");
            } else if (next.getLastModifiedTime().longValue() < Long.valueOf(Long.parseLong(next.getVersion())).longValue()) {
                next.setVersionDesc("有更新版本");
                HashMap<String, Long> hashMap = this.localFileInfoMaps;
                if (hashMap != null && hashMap.containsKey(file2.getAbsolutePath())) {
                    if (this.localFileInfoMaps.get(file2.getAbsolutePath()).longValue() < Long.valueOf(Long.parseLong(next.getVersion())).longValue()) {
                        next.setVersionDesc("有更新版本");
                    } else {
                        next.setVersionDesc("已是最新版本");
                    }
                }
            } else {
                next.setVersionDesc("已是最新版本");
            }
        }
        this.handler.sendEmptyMessage(1);
    }

    @Override // com.gisocn.onemap.view.OfflineDownloadView
    public void getDataFail(String str) {
        hideLoading();
        showToast(str);
    }

    @Override // com.gisocn.onemap.base.BaseFragment, com.gisocn.onemap.base.BaseView
    public void showLoading() {
        showProgressDialog();
        Log.e("showLoading", "showLoading");
    }

    @Override // com.gisocn.onemap.base.BaseFragment, com.gisocn.onemap.base.BaseView
    public void hideLoading() {
        dismissProgressDialog();
        Log.e("hideLoading", "hideLoading");
    }

    void updateCounts() {
        Iterator<Download.GroupItem> it = this.groupItemList.iterator();
        int i = 0;
        while (it.hasNext()) {
            Iterator<Download.NewItem> it2 = it.next().getItems().iterator();
            while (it2.hasNext()) {
                if (!"已是最新版本".equals(it2.next().getVersionDesc())) {
                    i++;
                }
            }
        }
        if (getActivity() != null) {
            ((MainActivity) getActivity()).setUpdateCounts(i);
        }
    }

    boolean deleteLocalData(String str) {
        if (str.contains(".tpk")) {
            File file = new File(Constant.SAVE_PATH_TPK, str);
            if (file.exists()) {
                return file.delete();
            }
            return true;
        }
        FileUtils.deleteFile(Constant.SAVE_PATH_SHP);
        return true;
    }
}
